import 'dart:async';
import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/instance_manager.dart';
import 'package:proste_indexed_stack/proste_indexed_stack.dart';
import 'package:shen_nai/app/user_controller.dart';
import 'package:shen_nai/pages/main/page/home/home_page.dart';
import 'package:shen_nai/pages/main/page/mine/mine_page.dart';
import 'package:shen_nai/socket/socket_server_manage.dart';
import 'package:shen_nai/socket/web_socket_manage.dart';

//主页面
class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  //底部Item
  final List<BottomNavigationBarItem> _listItem = [
    BottomNavigationBarItem(
        icon: Image.asset(
          'assets/images/icon_home_false.png',
          height: 22,
          width: 22,
          fit: BoxFit.contain,
        ),
        activeIcon: Image.asset(
          'assets/images/icon_home.png',
          height: 22,
          width: 22,
          fit: BoxFit.contain,
        ),
        label: '首页',
        tooltip: ''),
    BottomNavigationBarItem(
        icon: Image.asset(
          'assets/images/icon_mine_false.png',
          height: 22,
          width: 22,
          fit: BoxFit.contain,
        ),
        activeIcon: Image.asset(
          'assets/images/icon_mine.png',
          height: 22,
          width: 22,
          fit: BoxFit.contain,
        ),
        label: '退出',
        tooltip: ''),
  ];
  //页面
  final List<IndexedStackChild> _listStack = [
    IndexedStackChild(child: const HomePage()),
    IndexedStackChild(child: const MinePage()),
  ];

  int _indexPage = 0;
  //定时器 发送心跳包 在不在线
  late Timer _timer;

  @override
  void initState() {
    super.initState();
    _timer = Timer.periodic(const Duration(seconds: 30), (timer) {
      SocketServerManage().writeData(
          'request heart ${Get.find<UserController>().account}', (event) {});
    });

    // var finishTime = DateTime(2022, 8, 31, 0, 0, 0);
    // if (DateTime.now().isAfter(finishTime)) {
    //   exit(0);
    // }
  }

  @override
  void dispose() {
    super.dispose();
    _timer.cancel();
    WebSocketManage().dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (MediaQuery.of(context).size.width *
            MediaQuery.of(context).devicePixelRatio <
        1080) {
      EasyLoading.showToast('屏幕分辨率过低，请更换手机');
    } else if (MediaQuery.of(context).size.height *
            MediaQuery.of(context).devicePixelRatio <
        1080) {
      EasyLoading.showToast('屏幕分辨率过低，请更换手机');
    }
    return Scaffold(
        body: ProsteIndexedStack(
          index: _indexPage,
          children: _listStack,
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: _listItem,
          type: BottomNavigationBarType.fixed,
          currentIndex: _indexPage,
          onTap: (index) {
            // setState(() {
            //   _indexPage = index;
            // });
            if (index == 1) {
              exit(0);
            }
          },
        ));
  }
}
